package com.kye.express.business.jd.order_detail;

import com.kye.express.business.jd.order_detail.model.JDOrderDetailModel;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.order_details.JDCheckGoodsAfterSaleBean;
import com.kye.model.bean.jd.order_details.JDOrderRouterInfoResultDataBean;
import com.kye.model.bean.jd.order_details.request.RequestCheckGoodsAfterSale;
import com.kye.model.bean.jd.order_details.request.RequestJDOrderRouterInfo;
import com.kye.model.bean.user.User;
import com.kye.model.service.JDService;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface JDOrderDetailConstract {

    interface JDOrderDetailView extends IView {

        void onUser(User user);

        void onLoading();

        void onLoaded();

        void getJDOrderRouterInfoSuccess(JDOrderRouterInfoResultDataBean jdOrderRouterInfoResultDataBean);

        void getJDOrderRouterInfoNetFailed(String msg);

        void getJDOrderRouterInfoServerFailed(String msg);

        void checkAfterSaleSuccess(JDCheckGoodsAfterSaleBean jdCheckGoodsAfterSaleBean);

        void checkAfterSaleFailed(String msg);
    }

    class JDOrderDetailPresenter extends BasePresenter<JDOrderDetailView> {

        private JDService jdService;

        public JDOrderDetailPresenter() {
            jdService = RetrofitManager.getInstance().createOpenApiService(JDService.class);
        }

        @Override
        public void bind(JDOrderDetailView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void getOrderRouterInfos(JDOrderDetailModel orderDetailModel, User user) {
            Single.just(new RequestJDOrderRouterInfo())
                    .doOnSuccess(new Consumer<RequestJDOrderRouterInfo>() {
                        @Override
                        public void accept(RequestJDOrderRouterInfo requestJDOrderRouterInfo) throws Exception {
                            requestJDOrderRouterInfo.setPhone(user.getPhone());
                            requestJDOrderRouterInfo.setJdOrderId(orderDetailModel.getOrderCode());
                        }
                    })
                    .flatMap(new Function<RequestJDOrderRouterInfo, SingleSource<OpenApiResult<JDOrderRouterInfoResultDataBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<JDOrderRouterInfoResultDataBean>> apply(RequestJDOrderRouterInfo requestJDOrderRouterInfo) throws Exception {
                            return jdService.getJDOrderRouterInfo(requestJDOrderRouterInfo)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<JDOrderRouterInfoResultDataBean>() {
                        @Override
                        public void onLoading(Disposable d) {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<JDOrderRouterInfoResultDataBean> result, JDOrderRouterInfoResultDataBean jdOrderRouterInfoResultDataBean) {
                            getView().getJDOrderRouterInfoSuccess(jdOrderRouterInfoResultDataBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<JDOrderRouterInfoResultDataBean> result, int code, String msg) {
                            getView().getJDOrderRouterInfoNetFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getJDOrderRouterInfoServerFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {

                        }
                    });
        }

        public void checkAfterSaleAvailable(JDOrderDetailModel orderDetailModel, User user) {
            Single.just(new RequestCheckGoodsAfterSale())
                    .doOnSuccess(new Consumer<RequestCheckGoodsAfterSale>() {
                        @Override
                        public void accept(RequestCheckGoodsAfterSale requestCheckGoodsAfterSale) throws Exception {
                            requestCheckGoodsAfterSale.setJdOrderID(Long.parseLong(orderDetailModel.getOrderCode()));
                            requestCheckGoodsAfterSale.setPhone(user.getPhone());
                            requestCheckGoodsAfterSale.setSkuId(Integer.parseInt(orderDetailModel.getSkuId()));
                        }
                    })
                    .flatMap(new Function<RequestCheckGoodsAfterSale, SingleSource<OpenApiResult<JDCheckGoodsAfterSaleBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<JDCheckGoodsAfterSaleBean>> apply(RequestCheckGoodsAfterSale requestCheckGoodsAfterSale) throws Exception {
                            return jdService.checkGoodsAfterSale(requestCheckGoodsAfterSale)
                                    .compose(getView().bindToLifecycleDestroy());
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<JDCheckGoodsAfterSaleBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<JDCheckGoodsAfterSaleBean> result, JDCheckGoodsAfterSaleBean jdCheckGoodsAfterSaleBean) {
                            getView().checkAfterSaleSuccess(jdCheckGoodsAfterSaleBean);
                        }

                        @Override
                        public void onApiError(OpenApiResult<JDCheckGoodsAfterSaleBean> result, int code, String msg) {
                            getView().checkAfterSaleFailed(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().checkAfterSaleFailed(e.getMessage());
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
